package p2pscrabble;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JOptionPane;

public class MenuBar extends JMenuBar implements ActionListener {
	Game game;
	
	public MenuBar(Game game) {
		this.game = game;
		
		JMenu fileMenu = new JMenu();
		JMenu helpMenu = new JMenu();

		JMenuItem newGameMenu = new JMenuItem();
		JMenuItem saveGameMenu = new JMenuItem();
		JMenuItem exitMenuItem = new JMenuItem();
		JMenuItem aboutMenuItem = new JMenuItem();

		fileMenu.setText("File");

		newGameMenu.setText("New Game");
		fileMenu.add(newGameMenu);
		
		// save game
		saveGameMenu.setText("Save Game");
		saveGameMenu.setActionCommand("save");
		saveGameMenu.addActionListener(this);
		fileMenu.add(saveGameMenu);

		// exit game
		exitMenuItem.setText("Exit");
		exitMenuItem.setActionCommand("exit");
		exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_F4,
				java.awt.event.InputEvent.ALT_MASK));
		exitMenuItem.addActionListener(this);
		fileMenu.add(exitMenuItem);

		this.add(fileMenu);
		aboutMenuItem.setText("About");

		helpMenu.setText("Help");
		helpMenu.add(aboutMenuItem);

		this.add(helpMenu);
	}

	public void exit_action(final ActionEvent e) {
		int exitConfirm;
		exitConfirm = JOptionPane.showConfirmDialog(null, "Game on progress.\n"
				+ "If you exit, you are counted as surrender.\n"
				+ "Exit this program?", "Confirm Exit",
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		// System.out.println(exitConfirm);
		if (exitConfirm == 0) {
			game.end(2, 0, 0);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getActionCommand().equals("exit")) {
			exit_action(null);
		}
		else if (arg0.getActionCommand().equals("save")) {
			game.saveGame();
		}
	}
}
